//
//  YZMOneStockBuyInfoView.m
//  YZMicroStock
//
//  Created by apple on 16/12/6.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMOneStockBuyInfoView.h"
#import "YZMConfigureService.h"
#import "NSNumber+Utils.h"
#import "YZMStockUnitView.h"
#import "YZMRecommandView.h"
#import "NSNumber+Utils.h"
#import "DTTimePeriod+Utils.h"


@interface YZMOneStockBuyInfoView()
@property(nonatomic, strong) UILabel *marketPrice;
@property(nonatomic, strong) UILabel *frontPriceLabel;
@property(nonatomic, strong) UILabel *limitTimeLabel;

@property (nonatomic, strong) UILabel *labelWarning;
@property (nonatomic, strong) UILabel *zhiyingLabel;

@end

@implementation YZMOneStockBuyInfoView

- (instancetype)init {
    
    if (self = [super init]) {
        
        self.backgroundColor = [YZMAppStyle colorForMain];
        [self createObjects];
    }
    
    return self;
}

- (void)createObjects {
    
    __weak __typeof(&*self) weakSelf = self;
    
    UIView *buyBgView = [[UIView alloc] init];
    [self addSubview:buyBgView];
    [buyBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.equalTo(weakSelf);
        make.height.mas_equalTo(46);
    }];
    
    UILabel *buyPrice = [YZMAppStyle labelWithMediumWhite];
    buyPrice.text = @"点买金额（￥）";
    [buyBgView addSubview:buyPrice];
    [buyPrice mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(buyBgView).offset(15);
        make.centerY.equalTo(buyBgView);
    }];
    
    _marketPrice = [YZMAppStyle labelWithMediumLightGray];
    
    _marketPrice.font=[YZMAppStyle largeSizeBoldFont];
    _marketPrice.textColor=[YZMAppStyle colorForRiseText];
    _marketPrice.text = @"2000";
    [buyBgView addSubview:_marketPrice];
    [_marketPrice mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(buyBgView).offset(-15);
        make.centerY.equalTo(buyBgView);
    }];
    
    _frontPriceLabel=[YZMAppStyle labelWithSmallGray];
    NSString *numberString=[NSString stringWithFormat:@"%@",@(self.amountNumber)];
    NSString *frontPriceLabelString=[NSString stringWithFormat:@"(可买入%@股)",numberString];
    NSMutableAttributedString *priceString=[[NSMutableAttributedString alloc]initWithString:frontPriceLabelString];
    [priceString addAttribute:NSForegroundColorAttributeName value:[YZMAppStyle colorForWhite] range:NSMakeRange(4, numberString.length)];
    _frontPriceLabel.attributedText=priceString;
    [buyBgView addSubview:_frontPriceLabel];
    [_frontPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_marketPrice);
        make.right.equalTo(_marketPrice.mas_left).offset(-15);
    }];

    UIView *lineView = [YZMAppStyle splitLine];
    [buyBgView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.left.right.equalTo(buyBgView);
    }];
    
    //持仓时间
    YZMStockUnitView *timeView = [YZMStockUnitView new];
    timeView.title = @"持仓时间";

    UILabel *chargeLB = [YZMAppStyle labelWithMediumGray];
    chargeLB.text = @"2天";
    timeView.rightView = chargeLB;
    timeView.topLineType = YZMStockUnitLineTypeForMarginLeft;
    timeView.bottomLineType = YZMStockUnitLineTypeForMarginLeft;

    [self addSubview:timeView];
    [timeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView.mas_bottom);
        make.left.right.equalTo(self);
    }];
    
    //触发止盈
    YZMStockUnitView *zhiyingView = [YZMStockUnitView new];
    zhiyingView.title = @"触发止盈";
    
    _zhiyingLabel = [YZMAppStyle labelWithMediumGray];
    _zhiyingLabel.text = @"20%";
    zhiyingView.rightView = _zhiyingLabel;
    zhiyingView.topLineType = YZMStockUnitLineTypeForMarginLeft;
    zhiyingView.bottomLineType = YZMStockUnitLineTypeForMarginLeft;
    
    [self addSubview:zhiyingView];
    [zhiyingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(timeView.mas_bottom);
        make.left.right.equalTo(self);
    }];
    
    //交易综合费
    YZMStockUnitView *togetherView = [YZMStockUnitView new];
    togetherView.title = @"交易综合费";
    
    UILabel *togetherLabel = [YZMAppStyle labelWithMediumWhite];
    togetherLabel.font=[YZMAppStyle boldFontWithSize:16];
    NSMutableAttributedString *togetherString=[[NSMutableAttributedString alloc]initWithString:@"0元"];
    [togetherString addAttribute:NSForegroundColorAttributeName value:[YZMAppStyle colorForYellowButton] range:NSMakeRange(0, 1)];
    togetherLabel.attributedText=togetherString;
    togetherView.rightView = togetherLabel;
    togetherView.topLineType = YZMStockUnitLineTypeForMarginLeft;
    togetherView.bottomLineType = YZMStockUnitLineTypeForMarginLeft;

    [self addSubview:togetherView];
    [togetherView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(zhiyingView.mas_bottom);
        make.left.right.equalTo(self);
    }];
    
    //交易综合费
    YZMStockUnitView *lastrView = [YZMStockUnitView new];
    lastrView.title = @"履约保证金";
    
    UILabel *lastLabel = [YZMAppStyle labelWithMediumWhite];
    lastLabel.font=[YZMAppStyle boldFontWithSize:16];
    NSMutableAttributedString *lastString=[[NSMutableAttributedString alloc]initWithString:@"1元"];
    lastLabel.tag = 1000;
    [lastString addAttribute:NSForegroundColorAttributeName value:[YZMAppStyle colorForYellowButton] range:NSMakeRange(0, 1)];
    lastLabel.attributedText=lastString;
    lastrView.rightView = lastLabel;
    lastrView.topLineType = YZMStockUnitLineTypeForMarginLeft;
    lastrView.bottomLineType = YZMStockUnitLineTypeForMarginLeft;

    [self addSubview:lastrView];
    [lastrView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(togetherView.mas_bottom);
        make.left.right.equalTo(self);
    }];
    
    _limitTimeLabel=[YZMAppStyle labelWithSmallGray];
    [self addSubview:_limitTimeLabel];
    _limitTimeLabel.text=@"点买时间段：交易日  --";
    [_limitTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(lastrView.mas_bottom).offset(5);
    }];
    _labelWarning = [YZMAppStyle labelWithMediumRise];
    _labelWarning.hidden = NO;
    _labelWarning.preferredMaxLayoutWidth = ScreenWidth - 30;
    _labelWarning.numberOfLines = 0;
    _labelWarning.text = @"本次免费体验活动所获得的盈利分成将以红包的形式发放至您的红包账户，用于抵扣交易综合费";
    [self addSubview:_labelWarning];
    [_labelWarning mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(_limitTimeLabel.mas_bottom).offset(5);
    }];
}

- (void)setIsFree:(BOOL)isFree{
    UILabel *label = [self viewWithTag:1000];
    NSMutableAttributedString *lastString=[[NSMutableAttributedString alloc]initWithString:isFree?@"0元":@"1元"];
    [lastString addAttribute:NSForegroundColorAttributeName value:[YZMAppStyle colorForYellowButton] range:NSMakeRange(0, 1)];
    label.attributedText = lastString;
    _labelWarning.hidden = !isFree;
}

-(void)updateDate{
    
    NSString *numberString=[NSString stringWithFormat:@"%@",@(self.amountNumber)];
    NSString *frontPriceLabelString=[NSString stringWithFormat:@"(可买入%@股)",numberString];
    NSMutableAttributedString *priceString=[[NSMutableAttributedString alloc]initWithString:frontPriceLabelString];
    [priceString addAttribute:NSForegroundColorAttributeName value:[YZMAppStyle colorForWhite] range:NSMakeRange(4, numberString.length)];
    _frontPriceLabel.attributedText=priceString;
    _marketPrice.text = [NSString stringWithFormat:@"%.2f",_defaultModel.totalMoney];
    _zhiyingLabel.text = [[NSString stringWithFormat:@"%.0f",_defaultModel.quitGainRatio * 100] stringByAppendingString:@"%"];
    NSArray *timeArray=_defaultModel.limitTimePeriods;
    if (timeArray) {
        DTTimePeriod *startTime=[timeArray firstObject];
        DTTimePeriod *endTime = [timeArray lastObject];
        
        NSString *firstStartString=[startTime.StartDate formattedDateWithFormat:@"HH:mm"];
        NSString *firstEndString=[startTime.EndDate formattedDateWithFormat:@"HH:mm"];
        
        NSString *secondStartString=[endTime.StartDate formattedDateWithFormat:@"HH:mm"];
        NSString *secondEndString = [endTime.EndDate formattedDateWithFormat:@"HH:mm"];
      
        NSString *timeLimitString=[NSString stringWithFormat:@"点买时间段：交易日 %@,%@|%@,%@",firstStartString,firstEndString,secondStartString,secondEndString];
        
        _limitTimeLabel.text=timeLimitString;
    }

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
